<?php
/**
 * @copyright : ienter asia Technologies
 * @author :  HungLV
 * @version    Release: V1.0
 * @Description : Class Module User   
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Mod_user extends MYAD_Controller{
/*
 * @author :        HungLV
 * @created:        
 * @Description:    Load library Model User
 * @Input :         Null
 * @Output :        Null
 */    
    public function __construct(){
        parent::__construct();
		$this->load->model('Mmod_user');
    }
/*
 * @author :        HungLV
 * @created:        
 * @Description:    Get all user data
 * @Input :         $page = 0
 * @Output :        Return array conversion for data view
 */ 
    public function index($page = 0){
        $data = $this->Mmod_user->getAllData($page);
        $this->load->view('layout',$data);
    }
/*
 * @author :        HungLV
 * @created:        
 * @Description:    Registration and Edit User
 * @Input :         Null
 * @Output :        Null
 */     
    function updateUser(){
       $temp = array();
       $name_action = array();
       $id = $this->input->post('id');
       // check before edit
       if($this->uri->segment(2)=='edit'){
            if($id!='' && $this->Mmod_user->getUser($id)==FALSE){
                redirect('/user');
            }
            if($id==''){
                redirect('/user');
            }
       }
        if($this->input->post('btnSubmit')){
            if($id!=null&&is_numeric($id)&&$this->Mmod_user->getUser($id)){
                 $validateFlag = $this->Mmod_user->validate_edit();
                  if($validateFlag){
                      $id = $this->input->post('id');
                      $temp = $this->Mmod_user->updateUserData($id);
                  }
            }
            else{
                 $validateFlag = $this->Mmod_user->validate();
                  if($validateFlag){
                      $temp = $this->Mmod_user->updateUserData();
                  }               
            }
         }
         if($this->uri->segment(2)=='edit'){
             $name_action['name_view'] = 'edit';
         }
         else{
             $name_action['name_view'] = 'add';
         }
        $data = $this->Mmod_user->getAddUser($id);
        $this->load->view('layout', array_merge($data, $temp,$name_action));       
    }
    /*
     * @author :        Hunglv
     * @created:        
     * @Description:    Validate select level
     * @Input :         
     * @Output :        return true or false
     */

    function select_level($str) {
        if ($str != 0) {
            return TRUE;
        } else {
            $this->form_validation->set_message('select_level', '%s ' . $this->lang->line('User_required_select'));
            return FALSE;
        }
    }
    /*
     * @author :        Hunglv
     * @created:        
     * @Description:    Validate input character is Space
     * @Input :         
     * @Output :        return true or false
     */

    function check_space($str) {
        $sub = ' ';
        if (strlen(strstr($str, $sub)) > 0) {
            $this->form_validation->set_message('check_space', '%s ' . $this->lang->line('Home_containing_space'));
            return FALSE;
        } else {
            return TRUE;
        }
    }
    /*
     * @author :        Hunglv
     * @created:        
     * @Description:    Validate input character is Space
     * @Input :         
     * @Output :        return true or false
     */

    function check_matches($str,$txtPassword='') {
        if($str!=$txtPassword){
            $this->form_validation->set_message('check_matches',$this->lang->line('matches'));
            return FALSE;            
        }
        else{
            return TRUE;
        }
    }    
    /*
     * @author :        Hunglv
     * @created:        
     * @Description:    check duplicate user name
     * @Input :         
     * @Output :        return true or false
     */

    function check_UserName($userName='',$userId='') {
        if (empty($userName)) {
            return TRUE;
        }
        else{
            $result = $this->Mmod_user->getUserBy('UserName',$userName);
            if($this->input->post($userId)){
                $id = $this->input->post($userId);
                if(count($result)>0){
                    if($result['UserId']==$id){
                        return TRUE;
                    }
                    else{
                        $this->form_validation->set_message('check_UserName', '%s ' . $this->lang->line('Us_UserName_duplicate'));
                        return FALSE;                        
                    }            
                }
                else{
                    return TRUE;
                }                
            }
            else{
                if(count($result)>0){
                    $this->form_validation->set_message('check_UserName', '%s ' . $this->lang->line('Us_UserName_duplicate'));
                    return FALSE;                                  
                }
                else{
                    return TRUE;
                }                 
            }
        }
    } 
    /*
     * @author :        Hunglv
     * @created:        
     * @Description:    check duplicate email
     * @Input :         
     * @Output :        return true or false
     */

    function check_Email($email='',$userId='') {
        if (empty($email)) {
            return TRUE;
        }
        else{
            $result = $this->Mmod_user->getUserBy('Email',$email);
            if($this->input->post($userId)){
                $id = $this->input->post($userId);
                if(count($result)>0){
                    if($result['UserId']==$id){
                        return TRUE;
                    }
                    else{
                        $this->form_validation->set_message('check_Email', '%s ' . $this->lang->line('Us_UserName_duplicate'));
                        return FALSE;                        
                    }            
                }
                else{
                    return TRUE;
                }                
            }
            else{
                if(count($result)>0){
                    $this->form_validation->set_message('check_Email', '%s ' . $this->lang->line('Us_UserName_duplicate'));
                    return FALSE;                                  
                }
                else{
                    return TRUE;
                }                 
            }
        }
    }    
}
?>